package com.base.cn.platform.os.controller.course.open;

import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.course.CourseOpenForm;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.open.CourseOpenBespeakService;
import com.base.cn.platform.os.service.course.open.CourseOpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 免费直播接口Controller
 *
 * @author s.li
 * @create 2018-09-07-9:33
 */
@RestController
@RequestMapping("/api/openCourse")
public class ApiOpenCourseController extends ApiBaseController{

    @Autowired
    private CourseOpenService courseOpenService;
    @Autowired
    private CourseOpenBespeakService courseOpenBespeakService;

    /**
     * 查询免费直播列表接口
     * @param year 年份
     * @param month 月份
     * @param date 日期
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/queryOpenList")
    public Map<String,Object> queryOpenList(@RequestParam("year") int year,
                                            @RequestParam("month") int month,
                                            @RequestParam("date") int date,
                                            @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        HashMap<String, Object> condition = new HashMap<>();
        condition.put("nowDate", DateUtils.parseDate(year+"-"+month +"-" +date,"yyyy-MM-dd"));
        condition.put("status",1);
        Map<String, Object> courseOpenList = courseOpenService.findCourseOpenPage(condition, currentPage, null,false, true, false,false);
        return ResultUtil.SUCCESS(courseOpenList);
    }

    /**
     * 预约报名免费直播
     * @param userId 用户ID
     * @param openId 免费直播ID
     * @return Map<String,Object>
     */
    @RequestMapping("/bespeakOpen")
    public Map<String,Object> bespeakOpen(@RequestParam("userId") BigDecimal userId,
                                          @RequestParam("openId") BigDecimal openId){
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("openCourseId",openId);
        return courseOpenBespeakService.saveBespeak(params);
    }

    /**
     * 播放免费直播
     * @param userId 用户ID
     * @param openId 免费直播ID
     * @return  Map<String,Object>
     */
    @RequestMapping("/playOpenCourse")
    public Map<String,Object> playOpenLive(@RequestParam("userId") BigDecimal userId,
                                           @RequestParam("openId") BigDecimal openId){
        return courseOpenService.playOpenLive(openId,userId, CourseOpenForm.APP);
    }
}
